package com.youth.orderservice.mq.Config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class createOrderConfigMQ {

    //交换机队列名字
    public static final String X_EXCHANGE = "X";
    public static final String Z_EXCHANGE = "Z";
    public static final String DELAY_QUEUE = "QA";
    public static final String D_QUEUE = "QD";

    //交换机声明
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    /*交换机声明*/
    @Bean("zExchange")
    public DirectExchange zExchange(){
        return new DirectExchange(Z_EXCHANGE);
    }

    //延时队列声明
    @Bean("queueA")
    public Queue queueA(){
        HashMap<String,Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Z_EXCHANGE);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        args.put("x-message-ttl", 600000);
        return QueueBuilder.durable(DELAY_QUEUE).withArguments(args).build();
    }

    //声明死信队列 QD
    @Bean("queueD")
    public Queue queueD(){
        return new Queue(D_QUEUE);
    }

    //将交换机和队列进行绑定
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean
    public Binding queueDBindingZ(@Qualifier("queueD") Queue queueA,
                                  @Qualifier("zExchange") DirectExchange zExchange){
        return BindingBuilder.bind(queueA).to(zExchange).with("YD");
    }

}















